home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / ColorBal.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  10.2 KB  |  324 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmBright 
  4.    Caption         =   "ColorBal []"
  5.    ClientHeight    =   3960
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   5160
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   3960
  11.    ScaleWidth      =   5160
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.CommandButton cmdRefresh 
  14.       Caption         =   "Refresh"
  15.       Height          =   375
  16.       Left            =   3840
  17.       TabIndex        =   11
  18.       Top             =   360
  19.       Width           =   855
  20.    End
  21.    Begin VB.HScrollBar hbarBrightness 
  22.       Height          =   255
  23.       Index           =   2
  24.       Left            =   720
  25.       Max             =   100
  26.       Min             =   -100
  27.       TabIndex        =   8
  28.       Top             =   720
  29.       Width           =   2415
  30.    End
  31.    Begin VB.HScrollBar hbarBrightness 
  32.       Height          =   255
  33.       Index           =   1
  34.       Left            =   720
  35.       Max             =   100
  36.       Min             =   -100
  37.       TabIndex        =   5
  38.       Top             =   420
  39.       Width           =   2415
  40.    End
  41.    Begin VB.HScrollBar hbarBrightness 
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   720
  45.       Max             =   100
  46.       Min             =   -100
  47.       TabIndex        =   2
  48.       Top             =   120
  49.       Width           =   2415
  50.    End
  51.    Begin MSComDlg.CommonDialog dlgOpenFile 
  52.       Left            =   4560
  53.       Top             =   0
  54.       _ExtentX        =   847
  55.       _ExtentY        =   847
  56.       _Version        =   393216
  57.    End
  58.    Begin VB.PictureBox picOriginal 
  59.       AutoSize        =   -1  'True
  60.       Height          =   2775
  61.       Left            =   120
  62.       ScaleHeight     =   181
  63.       ScaleMode       =   3  'Pixel
  64.       ScaleWidth      =   157
  65.       TabIndex        =   1
  66.       Top             =   1080
  67.       Width           =   2415
  68.    End
  69.    Begin VB.PictureBox picResult 
  70.       Height          =   2775
  71.       Left            =   2640
  72.       ScaleHeight     =   181
  73.       ScaleMode       =   3  'Pixel
  74.       ScaleWidth      =   157
  75.       TabIndex        =   0
  76.       Top             =   1080
  77.       Width           =   2415
  78.    End
  79.    Begin VB.Label Label1 
  80.       Caption         =   "Blue"
  81.       Height          =   255
  82.       Index           =   2
  83.       Left            =   120
  84.       TabIndex        =   10
  85.       Top             =   720
  86.       Width           =   495
  87.    End
  88.    Begin VB.Label lblBrighhtness 
  89.       Alignment       =   1  'Right Justify
  90.       BorderStyle     =   1  'Fixed Single
  91.       Caption         =   "0"
  92.       Height          =   255
  93.       Index           =   2
  94.       Left            =   3240
  95.       TabIndex        =   9
  96.       Top             =   720
  97.       Width           =   495
  98.    End
  99.    Begin VB.Label Label1 
  100.       Caption         =   "Green"
  101.       Height          =   255
  102.       Index           =   0
  103.       Left            =   120
  104.       TabIndex        =   7
  105.       Top             =   420
  106.       Width           =   495
  107.    End
  108.    Begin VB.Label lblBrighhtness 
  109.       Alignment       =   1  'Right Justify
  110.       BorderStyle     =   1  'Fixed Single
  111.       Caption         =   "0"
  112.       Height          =   255
  113.       Index           =   1
  114.       Left            =   3240
  115.       TabIndex        =   6
  116.       Top             =   420
  117.       Width           =   495
  118.    End
  119.    Begin VB.Label lblBrighhtness 
  120.       Alignment       =   1  'Right Justify
  121.       BorderStyle     =   1  'Fixed Single
  122.       Caption         =   "0"
  123.       Height          =   255
  124.       Index           =   0
  125.       Left            =   3240
  126.       TabIndex        =   4
  127.       Top             =   120
  128.       Width           =   495
  129.    End
  130.    Begin VB.Label Label1 
  131.       Caption         =   "Red"
  132.       Height          =   255
  133.       Index           =   1
  134.       Left            =   120
  135.       TabIndex        =   3
  136.       Top             =   120
  137.       Width           =   495
  138.    End
  139.    Begin VB.Menu mnuFile 
  140.       Caption         =   "&File"
  141.       Begin VB.Menu mnuFileOpen 
  142.          Caption         =   "&Open..."
  143.          Shortcut        =   ^O
  144.       End
  145.       Begin VB.Menu mnuFileSaveAs 
  146.          Caption         =   "Save &As..."
  147.          Shortcut        =   ^A
  148.       End
  149.    End
  150. Attribute VB_Name = "frmBright"
  151. Attribute VB_GlobalNameSpace = False
  152. Attribute VB_Creatable = False
  153. Attribute VB_PredeclaredId = True
  154. Attribute VB_Exposed = False
  155. Option Explicit
  156. ' Arrange the controls.
  157. Private Sub ArrangeControls()
  158.     ' Position the result PictureBox.
  159.     picResult.Move _
  160.         picOriginal.Left + picOriginal.Width + 120, _
  161.         picOriginal.Top, _
  162.         picOriginal.Width, _
  163.         picOriginal.Height
  164.     picResult.Cls
  165.     ' This makes the image resize itself to
  166.     ' fit the picture.
  167.     picResult.Picture = picResult.Image
  168.     ' Make the form big enough.
  169.     Width = picResult.Left + picResult.Width + _
  170.         Width - ScaleWidth + 120
  171.     Height = picResult.Top + picResult.Height + _
  172.         Height - ScaleHeight + 120
  173.     DoEvents
  174. End Sub
  175. ' Transform the image.
  176. Private Sub TransformImage()
  177. Dim r_factor As Single
  178. Dim g_factor As Single
  179. Dim b_factor As Single
  180. Dim pixels() As RGBTriplet
  181. Dim bits_per_pixel As Integer
  182. Dim X As Integer
  183. Dim Y As Integer
  184.     ' Get the selected color values.
  185.     r_factor = hbarBrightness(0).value / 100#
  186.     g_factor = hbarBrightness(1).value / 100#
  187.     b_factor = hbarBrightness(2).value / 100#
  188.     ' Get the pixels from picOriginal.
  189.     GetBitmapPixels picOriginal, pixels, bits_per_pixel
  190.     ' Set the pixel colors.
  191.     For Y = 0 To picOriginal.ScaleHeight - 1
  192.         For X = 0 To picOriginal.ScaleWidth - 1
  193.             With pixels(X, Y)
  194.                 If r_factor < 0 Then
  195.                     .rgbRed = (1 + r_factor) * .rgbRed
  196.                 Else
  197.                     .rgbRed = .rgbRed + r_factor * (255 - .rgbRed)
  198.                 End If
  199.                 If g_factor < 0 Then
  200.                     .rgbGreen = (1 + g_factor) * .rgbGreen
  201.                 Else
  202.                     .rgbGreen = .rgbGreen + g_factor * (255 - .rgbGreen)
  203.                 End If
  204.                 If b_factor < 0 Then
  205.                     .rgbBlue = (1 + b_factor) * .rgbBlue
  206.                 Else
  207.                     .rgbBlue = .rgbBlue + b_factor * (255 - .rgbBlue)
  208.                 End If
  209.             End With
  210.         Next X
  211.     Next Y
  212.     ' Set picResult's pixels.
  213.     SetBitmapPixels picResult, bits_per_pixel, pixels
  214.     picResult.Picture = picResult.Image
  215. End Sub
  216. ' Transform the image.
  217. Private Sub cmdRefresh_Click()
  218.     If picResult.Picture <> 0 Then
  219.         Screen.MousePointer = vbHourglass
  220.         DoEvents
  221.         TransformImage
  222.         Screen.MousePointer = vbDefault
  223.     End If
  224. End Sub
  225. ' Start in the current directory.
  226. Private Sub Form_Load()
  227.     picOriginal.AutoSize = True
  228.     picOriginal.ScaleMode = vbPixels
  229.     picOriginal.AutoRedraw = True
  230.     picResult.ScaleMode = vbPixels
  231.     picResult.AutoRedraw = True
  232.     dlgOpenFile.CancelError = True
  233.     dlgOpenFile.InitDir = App.Path
  234.     dlgOpenFile.Filter = _
  235.         "Bitmaps (*.bmp)|*.bmp|" & _
  236.         "GIFs (*.gif)|*.gif|" & _
  237.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  238.         "Icons (*.ico)|*.ico|" & _
  239.         "Cursors (*.cur)|*.cur|" & _
  240.         "Run-Length Encoded (*.rle)|*.rle|" & _
  241.         "Metafiles (*.wmf)|*.wmf|" & _
  242.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  243.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  244.         "All Files (*.*)|*.*"
  245. End Sub
  246. ' Display the brightness value selected.
  247. Private Sub hbarBrightness_Change(Index As Integer)
  248.     lblBrighhtness(Index).Caption = Format$(hbarBrightness(Index).value)
  249. End Sub
  250. ' Display the brightness value selected.
  251. Private Sub hbarBrightness_Scroll(Index As Integer)
  252.     lblBrighhtness(Index).Caption = Format$(hbarBrightness(Index).value)
  253. End Sub
  254. ' Load the indicated file.
  255. Private Sub mnuFileOpen_Click()
  256. Dim file_name As String
  257.     ' Let the user select a file.
  258.     On Error Resume Next
  259.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  260.     dlgOpenFile.ShowOpen
  261.     If Err.Number = cdlCancel Then
  262.         Exit Sub
  263.     ElseIf Err.Number <> 0 Then
  264.         Beep
  265.         MsgBox "Error selecting file.", , vbExclamation
  266.         Exit Sub
  267.     End If
  268.     On Error GoTo 0
  269.     Screen.MousePointer = vbHourglass
  270.     DoEvents
  271.     file_name = Trim$(dlgOpenFile.FileName)
  272.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  273.         - Len(dlgOpenFile.FileTitle) - 1)
  274.     Caption = "ColorBal [" & dlgOpenFile.FileTitle & "]"
  275.     ' Open the original file.
  276.     On Error GoTo LoadError
  277.     picOriginal.Picture = LoadPicture(file_name)
  278.     On Error GoTo 0
  279.     ' Make picResult the same size and position it.
  280.     ArrangeControls
  281.     ' Transform the image.
  282.     TransformImage
  283.     Screen.MousePointer = vbDefault
  284.     Exit Sub
  285. LoadError:
  286.     Screen.MousePointer = vbDefault
  287.     MsgBox "Error " & Format$(Err.Number) & _
  288.         " opening file '" & file_name & "'" & vbCrLf & _
  289.         Err.Description
  290. End Sub
  291. ' Save the transformed image.
  292. Private Sub mnuFileSaveAs_Click()
  293. Dim file_name As String
  294.     ' Let the user select a file.
  295.     On Error Resume Next
  296.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  297.     dlgOpenFile.ShowSave
  298.     If Err.Number = cdlCancel Then
  299.         Exit Sub
  300.     ElseIf Err.Number <> 0 Then
  301.         Beep
  302.         MsgBox "Error selecting file.", , vbExclamation
  303.         Exit Sub
  304.     End If
  305.     On Error GoTo 0
  306.     Screen.MousePointer = vbHourglass
  307.     DoEvents
  308.     file_name = Trim$(dlgOpenFile.FileName)
  309.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  310.         - Len(dlgOpenFile.FileTitle) - 1)
  311.     Caption = "ColorBal [" & dlgOpenFile.FileTitle & "]"
  312.     ' Save the transformed image into the file.
  313.     On Error GoTo SaveError
  314.     SavePicture picResult.Picture, file_name
  315.     On Error GoTo 0
  316.     Screen.MousePointer = vbDefault
  317.     Exit Sub
  318. SaveError:
  319.     Screen.MousePointer = vbDefault
  320.     MsgBox "Error " & Format$(Err.Number) & _
  321.         " saving file '" & file_name & "'" & vbCrLf & _
  322.         Err.Description
  323. End Sub
  324.